package com.project.gjzb.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.gjzb.dto.ParamDto;
import com.project.gjzb.entity.TobjBusDirection;
import com.project.gjzb.mapper.TobjBusDirectionMapper;
import com.project.gjzb.service.BusDirectionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BusDirectionServiceImpl implements BusDirectionService {

    @Resource
    private TobjBusDirectionMapper tobjBusDirectionMapper;

    @Override
    @DS("datacenter")
    public List<TobjBusDirection> getList() {
        return tobjBusDirectionMapper.getList();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void saveList(List<TobjBusDirection> list) {
        for (TobjBusDirection entity : list) {
            tobjBusDirectionMapper.insertSelective(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteByDate(ParamDto paramDto) {
        tobjBusDirectionMapper.deleteByDate(paramDto);
    }

    @Override
    @DS("master")
    public String getDirectionById(String date, String id, String sxx) {
        String result = "";
        Example example = new Example(TobjBusDirection.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("rq", date);
        c.andEqualTo("isremove", 0);
        c.andEqualTo("busline_id", id);
        c.andEqualTo("directiontype", sxx);
        example.orderBy("directionlevel").asc();
        List<TobjBusDirection> list = tobjBusDirectionMapper.selectByExample(example);
        if (list.size() > 0) {
            result = list.stream().map(item -> item.getRoadname()).collect(Collectors.joining(","));
        } else {
            if ("0".equals(sxx)) {
                Example example2 = new Example(TobjBusDirection.class);
                Example.Criteria c2 = example2.createCriteria();
                c2.andEqualTo("rq", date);
                c2.andEqualTo("isremove", 0);
                c2.andEqualTo("busline_id", id);
                c2.andEqualTo("directiontype", "2");
                example2.orderBy("directionlevel").asc();
                List<TobjBusDirection> list2 = tobjBusDirectionMapper.selectByExample(example2);
                if (list2.size() > 0) {
                    result = list2.stream().map(item -> item.getRoadname()).collect(Collectors.joining(","));
                }
            }
        }
        return result;
    }
}
